//+------------------------------------------------------------------+
//|                                                         aaaa.mq5 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

//+------------------------------------------------------------------+
//| EA魔术棒参数设置                                                 |
//+------------------------------------------------------------------+
input int      MagicNumber       = 666;       // EA魔术棒
input double   LotSize           = 0.01;      // 交易手数
input int      TakeProfitPoints  = 1000;      // 止盈点数
input int      StepPoints        = 1000;      // 加仓间隔点数

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
   
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//--- 检查是否有未平仓单
   bool hasPosition = false;
   double lowestPrice = DBL_MAX;
   
//--- 遍历所有持仓
   for(int i=PositionsTotal()-1; i>=0; i--)
   {
      ulong posTicket = PositionGetTicket(i);
      if(posTicket > 0 && PositionSelectByTicket(posTicket))
      {
         if(PositionGetInteger(POSITION_MAGIC) == MagicNumber && 
            PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
         {
            hasPosition = true;
            double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
            if(openPrice < lowestPrice) lowestPrice = openPrice;
         }
      }
   }

//--- 交易逻辑
   if(!hasPosition)
   {
      OpenBuy(); // 无持仓时开首单
   }
   else
   {
      // 计算当前价格与最低持仓价的关系
      double currentBid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      if(currentBid <= lowestPrice - StepPoints * _Point)
      {
         OpenBuy(); // 满足加仓条件时加仓
      }
   }
}
//+------------------------------------------------------------------+
//| 开多单函数                                                       |
//+------------------------------------------------------------------+
void OpenBuy()
{
   MqlTradeRequest request = {};
   MqlTradeResult  result = {};
   
   double askPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double tpPrice = askPrice + TakeProfitPoints * _Point;

   request.action    = TRADE_ACTION_DEAL;
   request.symbol    = _Symbol;
   request.volume    = LotSize;
   request.type      = ORDER_TYPE_BUY;
   request.price     = askPrice;
   request.tp        = tpPrice;
   request.deviation = 5;
   request.magic     = MagicNumber;

//--- 发送交易请求
   if(!OrderSend(request, result))
   {
      Print("开单失败 错误代码：", GetLastError());
   }
}
//+------------------------------------------------------------------+